package sisi.unitmeasure;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * UnitmeasureController.java - Controller class for the <i>unit measure database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 19/11/2013
 * 
 */

public class UnitmeasureController { 

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Unitmeasure[] getPayment(String cerca) {
		List<Unitmeasure> unitmeasure = getUnitmeasureList(cerca);
		return unitmeasure.toArray(new Unitmeasure[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Unitmeasure> getUnitmeasureList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.descum) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Unitmeasure as c " + cWhere
							+ " order by c.um");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Unitmeasure> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getUMList1Campo() {

		String cWhere = "";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select DISTINCT c.um from Unitmeasure as c " + cWhere
							+ " order by c.um");
			List<String> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	

	public Unitmeasure getUnitmeasurexId(int id) {

		EntityManager em = getEntityManager();
		try {
			Unitmeasure unitmeasurex = em.find(Unitmeasure.class, id);
			return unitmeasurex;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Unitmeasure addUnitmeasure(Unitmeasure unitmeasure) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(unitmeasure);
			em.getTransaction().commit();
			unitmeasure = refreshUnitmeasure(unitmeasure);
		} finally {
			em.close();
			return unitmeasure;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateUnitmeasure(Unitmeasure unitmeasure) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Unitmeasure unitmeasurex = em.find(Unitmeasure.class, unitmeasure.getId());
			unitmeasurex.setUm(unitmeasure.getUm());
			unitmeasurex.setId(unitmeasure.getId());
			unitmeasurex.setDescum(unitmeasure.getDescum());			
			em.getTransaction().commit();
			em.flush();
		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Unitmeasure refreshUnitmeasure(Unitmeasure unitmeasure) {

		Unitmeasure unitmeasurex = unitmeasure;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			unitmeasurex = em.find(Unitmeasure.class, unitmeasure.getId());
			em.flush();
			em.refresh(unitmeasurex);
		} finally { 
			em.close();
			return unitmeasurex;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeUnitmeasure(Unitmeasure unitmeasure) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Unitmeasure unitmeasurex = em.find(Unitmeasure.class, unitmeasure.getId());
			em.remove(unitmeasurex);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
